//
//  SearchViewController.m
//  FoodCorner
//
//  Created by Summer on 16/3/9.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchHelper.h"
#import "MainListModel.h"
#import "FoodPracticeViewController.h"
@interface SearchViewController ()<UISearchBarDelegate,UISearchResultsUpdating,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UISearchController *searchController;
@property (nonatomic,strong) UISwipeGestureRecognizer *leftSwipe;
@end

@implementation SearchViewController
-(UITableView *)tableView
{
    if (!_tableView) {
        CGRect frame = self.view.frame;
        //frame.origin.y += 50;
        _tableView = [[UITableView alloc]initWithFrame:frame style:(UITableViewStylePlain)];
        
        //_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 50);
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    [self.searchController.searchBar sizeToFit];
    self.tableView.delegate = self;
    self.tableView.dataSource  = self;
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;
    //[self reloadDataMethod];
    self.searchController.searchBar.showsCancelButton = NO;
    [self.view addSubview:self.tableView];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemStop) target:self action:@selector(clickBackButtonAciotn)];
    self.leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(clickBackButtonAciotn)];
    _leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:_leftSwipe];
    
    
    
}
- (void)clickBackButtonAciotn
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)reloadDataMethod
{
    [[SearchHelper shareSearchHelper] requestdidFinishWithString:self.searchController.searchBar.text success:^{
        [self.tableView reloadData];
    }];
}

#pragma  mark ---tableView的代理方法和数据源方法实现
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[SearchHelper shareSearchHelper]numberOfDataInSearchArray];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier: @"cellID"];
    }
    
    MainListModel *model = [[SearchHelper shareSearchHelper] modelOfIndexInSearchArray:indexPath.row];
    
    cell.textLabel.text = model.title;
    return cell;
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title contains %@ ",searchController.searchBar.text];
//    //将查询到的结果装入结果数组
//    self.resultArray = [[self.dataArray filteredArrayUsingPredicate:predicate] mutableCopy];
//    [self.tableView reloadData];
    
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [[SearchHelper shareSearchHelper] requestdidFinishWithString:self.searchController.searchBar.text success:^{
        [self.tableView reloadData];
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodPracticeViewController  *foodVC = [[FoodPracticeViewController alloc]init];
    UINavigationController *foodNC = [[UINavigationController alloc]initWithRootViewController:foodVC];
    foodVC.listModel = [[SearchHelper shareSearchHelper] modelOfIndexInSearchArray:indexPath.row];
    [self presentViewController:foodNC animated:YES completion:nil];
}

@end
